class Solution {
public:

    int m, n;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};

    void dfs(vector<vector<char> >& grid, int i, int j)
    {
        if(grid[i][j] == '0')
        {
            return;
        }
        grid[i][j] = '0';

        for(int k = 0; k < 4; k++)
        {
            int a = i + dx[k], b = j + dy[k];
            if(a >= 0 && a < m && b >= 0 && b < n)
            {
                dfs(grid, a, b);
            }
        }
    }
    int solve(vector<vector<char> >& grid) {
        int sum = 0;
        m = grid.size(); n = grid[0].size();
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == '1')
                {
                    dfs(grid, i, j);
                    sum++;
                }
            }
        }
        return sum;
    }
};